package com.his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.his.dto.PrescriptionInsert;
import com.his.entity.*;
import com.his.mapper.MedicineItemRecordMapper;
import com.his.mapper.MedicinePrescriptionRecordMapper;
import com.his.service.IMedicinePrescriptionRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 成药处方表 服务实现类
 * </p>
 *
 * @author Demoon Liu
 * @since 2022-02-20
 */
@Service
public class MedicinePrescriptionRecordServiceImpl extends ServiceImpl<MedicinePrescriptionRecordMapper, MedicinePrescriptionRecord> implements IMedicinePrescriptionRecordService {

    @Resource
    private MedicinePrescriptionRecordMapper mapper;

    @Resource
    private MedicineItemRecordMapper itemRecordMapper;

    @Resource
    private MedicineItemRecordServiceImpl itemService;

    @Override
    public ResponseResult myInsert(PrescriptionInsert prescriptionInsert) {
        Long count = 1L;
        Integer code = 0;
        String message = "";

        MedicinePrescriptionRecord medicinePrescriptionRecord = prescriptionInsert.getMedicinePrescriptionRecord();
        Collection<MedicineItemRecord> list = prescriptionInsert.getList();

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Long staffId = loginUser.getUser().getId();

        try {
            count = mapper.getMaxId();
            if (null == count)
                count = 0L;
            medicinePrescriptionRecord.setId(count+1);
            medicinePrescriptionRecord.setStatus(1);
            medicinePrescriptionRecord.setType(1);
            medicinePrescriptionRecord.setCreatetime(LocalDateTime.now());
            medicinePrescriptionRecord.setCreatestaffid(staffId);
            if (!this.save(medicinePrescriptionRecord)) {
                throw new Exception();
            }
            // 删除旧的处方项
            itemRecordMapper.delete(new QueryWrapper<MedicineItemRecord>().eq("prescriptionid", medicinePrescriptionRecord.getId()));
            // 增加新的处方项 status 1代表未发药 2代表已发药
            count = itemRecordMapper.getMaxId();
            for (MedicineItemRecord item : list) {
                item.setId(++count);
                item.setStatus(1);
            }
            if (!itemService.saveBatch(list)) {
                throw new Exception();
            }



            message = "插入成功";
            code = 201;
        }
        catch (Exception err) {
            message = "插入失败";
            code = 400;
        }

        return new ResponseResult(code, message, medicinePrescriptionRecord);
    }

    @Override
    public ResponseResult myDelete(MedicinePrescriptionRecord medicinePrescriptionRecord) {
        Integer code = 0;
        String message = "";

        try {
            if (0 == itemRecordMapper.delete(new QueryWrapper<MedicineItemRecord>().eq("prescriptionid", medicinePrescriptionRecord.getId()))) {
                throw new Exception();
            }
            if (0 == mapper.deleteById(medicinePrescriptionRecord)) {
                throw new Exception();
            }

            message = "删除成功";
            code = 201;



        }
        catch (Exception err) {
            message = "删除失败";
            code = 400;
        }

        return new ResponseResult(code, message, null);
    }

    @Override
    public ResponseResult mySearchById(MedicinePrescriptionRecord medicinePrescriptionRecord) {
        Integer code = 0;
        String message = "";
        MedicinePrescriptionRecord result = null;

        try {
            result = mapper.selectOne(new QueryWrapper<MedicinePrescriptionRecord>().eq("id", medicinePrescriptionRecord.getId()));

            message = "根据Id查询成功";
            code = 201;



        }
        catch (Exception err) {
            message = "根据Id查询失败";
            code = 400;
        }

        return new ResponseResult(code, message, result);
    }

    @Override
    public ResponseResult mySearchByRegistrationId(MedicinePrescriptionRecord medicinePrescriptionRecord) {
        Integer code = 0;
        String message = "";
        IPage<MedicinePrescriptionRecord> page = new Page<MedicinePrescriptionRecord>(1, 10);
        IPage<MedicinePrescriptionRecord> result = null;

        try {
            result = mapper.selectPage(page, new QueryWrapper<MedicinePrescriptionRecord>().eq("registrationid", medicinePrescriptionRecord.getRegistrationid()));

            message = "根据诊断Id查询成功";
            code = 201;



        }
        catch (Exception err) {
            message = "根据诊断Id查询失败";
            code = 400;
        }

        return new ResponseResult(code, message, result);
    }
}
